package com.gee.spring.cloud.aerospace.simulate.core.satellite.telemetry;

import com.gee.spring.cloud.aerospace.simulate.core.util.DslUtil;
import com.gee.spring.cloud.aerospace.simulate.mgt.entity.SatelliteTelemetryParam;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author gepengjun
 * @since 2024/7/10 15:32
 */
@Slf4j
public class SateTmPkgDtoGeneratorTask implements TimerTask {

    private TmFrameGenerator tmFrameGenerator;

    private Map<String, String> tmParamMap;

    private List<SatelliteTelemetryParam> telemetryParamList;

    private final Map<String, byte[]> tmParamBytesMap = new HashMap<>();

    private ThreadPoolExecutor threadPoolExecutor;

    private final List<CompletableFuture<Void>> paramCalThreadList = new ArrayList<>();

    @Override
    public void run(Timeout timeout) throws Exception {
        telemetryParamList.forEach(telemetryParam -> {
            paramCalThreadList.add(CompletableFuture.runAsync(() -> {
                String paramPhiValue = tmParamMap.get(telemetryParam.getParamCode());
                //todo 执行遥测转换脚本，
                DslUtil.invokeStatic(telemetryParam.getParamCode());
            }, threadPoolExecutor));
        });
        try {
            CompletableFuture.allOf(paramCalThreadList.toArray(new CompletableFuture[0])).get(1, TimeUnit.SECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            log.error("遥测参数计算异常：", e);
        }

    }
}
